package com.photographysharing.controller;


import com.photographysharing.common.ThreadLocalOperation;
import com.photographysharing.dto.CommentDTO;
import com.photographysharing.dto.ReportDTO;
import com.photographysharing.entity.Categories;
import com.photographysharing.entity.Reports;
import com.photographysharing.exception.ServiceException;
import com.photographysharing.request.SelectByPaging;
import com.photographysharing.response.DatasResponse;
import com.photographysharing.response.MessageResponse;
import com.photographysharing.service.ReportsService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/report")
public class ReportController {

    private ReportsService reportsService;

    public ReportController(ReportsService reportsService) {
        this.reportsService = reportsService;
    }


    @RequestMapping("/add")
    public MessageResponse add(@RequestBody Reports reports){
        String userId = ThreadLocalOperation.getThreadLocalUserId();
        reports.setUserId(userId);
        try{
            if(reportsService.save(reports)){
                return new MessageResponse("举报成功!");
            }else{
                return new MessageResponse("举报失败!");
            }
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }

    @GetMapping("/handle")
    public MessageResponse handle(int id,int status){
        Reports report = reportsService.getById(id);
        if(report == null){
            return new MessageResponse("id错误!",false);
        }
        if(report.getStatus() != 0){
            return new MessageResponse("已处理!",false);

        }
        report.setStatus(status);
        if(reportsService.updateById(report)){
            return new MessageResponse("处理完毕!");
        }
        return new MessageResponse("处理失败!",false);
    }

    @GetMapping("/selectByPag")
    public DatasResponse<ReportDTO> selectByPag(SelectByPaging pag){
        return reportsService.selectByPag(pag);
    }
}
